Release 10.1A: OpenEdge Development:
Programming Interfaces


Writing a ProDataSet to XML

The following code sample writes a ProDataSet to an XML file. Here is a snippet of the setup code showing the ProDataSet definition. Note the NESTED options.

/* pi-tfx-writeSetup-4.i */ 
/* Definition of a ProDataSet. */ 
. 
. 
. 
DEFINE DATASET DSET FOR ttDept, ttEmp, ttFam, ttBene 
    DATA-RELATION DeptEmp FOR ttDept, ttEmp  
        RELATION-FIELDS(deptcode, deptcode) NESTED 
    DATA-RELATION EmpFam FOR ttEmp, ttFam  
        RELATION-FIELDS (empnum,empnum) NESTED 
    DATA-RELATION EmpBene FOR ttEmp,ttBene  
        RELATION-FIELDS (empnum,empnum) NESTED. 
. 
. 
. 

Here is a code sample for a simple XML write from a static ProDataSet.

/* pi-tfx-write-4.p */ 
/* Writes the data from a static ProDataSet to an XML file. */ 
{pi-tfx-parameterVarDefs.i} 
{pi-tfx-writeSetup-4.i} 
DEFINE VARIABLE returnValue AS LOGICAL. 
  
ASSIGN 
     cTargetType = "FILE" 
     cFile = "Dept400.xml"  
     lFormatted = TRUE 
     cEncoding = ? 
     cSchemaLocation = ? 
     lWriteSchema = FALSE 
     lMinSchema = FALSE 
     lWriteBeforeImage = FALSE. 
  
returnValue = DATASET DSET:WRITE-XML(cTargetType, cFile, lFormatted,  
                                     cEncoding, cSchemaLocation, lMinSchema,  
                                     lWriteBeforeImage). 

This example uses the NESTED option, and here’s a snippet of the resulting XML demonstrating what it does. Note that the ttEmp row associated with employee number 3 of department 400 is nested within ttDept row for department 400 and that the rows from the ttFam table associated with employee number 3 are included within the ttEmp element.

<?xml version="1.0"?> 
<DSET xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <ttDept> 
    <DeptCode>400</DeptCode> 
    <DeptName>Sales</DeptName> 
    <ttEmp> 
      <EmpNum>3</EmpNum> 
      <LastName>Smith</LastName> 
      <FirstName>Justine</FirstName> 
      <Address>1342 Atlantic Ave</Address> 
      <Address2>Apt 345b</Address2> 
      <City>Boston</City> 
      <State>MA</State> 
      <PostalCode>01834</PostalCode> 
      <HomePhone>617 333-3334</HomePhone> 
      <WorkPhone>800 787-8484</WorkPhone> 
      <DeptCode>400</DeptCode> 
      <Position>Sales Manager</Position> 
      <Birthdate>1960-01-08</Birthdate> 
      <StartDate>1997-06-19</StartDate> 
      <VacationDaysLeft>5</VacationDaysLeft> 
      <SickDaysLeft>4</SickDaysLeft> 
      <ttFam> 
        <EmpNum>3</EmpNum> 
        <RelativeName>Kelly Smith</RelativeName> 
        <Relation>Daughter</Relation> 
        <Birthdate>1993-08-18</Birthdate> 
        <CoveredOnBenefits>true</CoveredOnBenefits> 
        <BenefitDate>1997-06-19</BenefitDate> 
      </ttFam> 
      <ttFam> 
        <EmpNum>3</EmpNum> 
        <RelativeName>Mark Smith</RelativeName> 
        <Relation>Spouse</Relation> 
        <Birthdate>1960-01-08</Birthdate> 
        <CoveredOnBenefits>true</CoveredOnBenefits> 
        <BenefitDate>1998-02-21</BenefitDate> 
      </ttFam> 

Remove the NESTED keywords from the setup code and run it again. The resulting XML file now listed all rows from one table then all rows from the next table, and so on. Here’s a condensed version of the XML to highlight this point:

<ttdep> 
    <ttEmp> 
        <EmpNum>3</EmpNum> 
        ... 
    </ttEmp> 
    <ttEmp> 
        <EmpNum>6</EmpNum> 
        ... 
    </ttEmp> 
    . 
    . 
    . 
    <ttFam> 
        <EmpNum>3</EmpNum> 
        ... 
    </ttFam> 
    <ttFam> 
        <EmpNum>3</EmpNum> 
        ... 
    </ttFam> 
    <ttFam> 
        <EmpNum>3</EmpNum> 
        ...  
    </ttFam> 
    <ttFam> 
        <EmpNum>6</EmpNum> 
        ...  
    </ttFam> 
    . 
    . 
    . 
</ttDept> 


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095